package com.dj.mingkong.portal.biz.controller.music;

import com.dj.mingkong.common.annotation.RequestLimit;
import com.dj.mingkong.common.infrastructure.base.dto.delete.DeleteVo;
import com.dj.mingkong.common.infrastructure.base.dto.page.PageVo;
import com.dj.mingkong.common.infrastructure.base.dto.rest.RestResponse;
import com.dj.mingkong.common.microservice.UserContext;
import com.dj.mingkong.common.util.AssertUtils;
import com.dj.mingkong.portal.biz.controller.music.vo.request.BoxAddVo;
import com.dj.mingkong.portal.biz.controller.music.vo.request.BoxQueryVo;
import com.dj.mingkong.portal.biz.controller.music.vo.request.BoxUpdateVo;
import com.dj.mingkong.portal.biz.controller.music.vo.response.BoxDetailVo;
import com.dj.mingkong.portal.biz.controller.music.vo.response.BoxVo;
import com.dj.mingkong.portal.biz.service.music.BoxService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 音乐盒管理
 *
 * @author zenglonghui
 * @date: 2022/8/31 20:53
 */
@RestController
@RequestMapping("/box")
public class BoxController {
    @Resource
    private BoxService boxService;

    /**
     * 新增音乐盒
     *
     * @param addVo 实例对象
     * @return 是否成功，true-成功，false-失败
     */
    @PostMapping("/add")
    @RequestLimit(count = 2)
    public RestResponse<Boolean> add(@RequestBody @Validated BoxAddVo addVo) {
        boxService.add(addVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 修改音乐盒
     *
     * @param updateVo 实例对象
     * @return 是否成功，true-成功，false-失败
     */
    @PostMapping("/updateById")
    @RequestLimit(count = 2)
    public RestResponse<Boolean> updateById(@RequestBody @Validated BoxUpdateVo updateVo) {
        boxService.updateById(updateVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 获取我的音乐盒列表
     *
     * @return BoxVo
     */
    @GetMapping("/listMyBox")
    @RequestLimit(count = 10)
    public RestResponse<List<BoxDetailVo>> listMyBox() {
        final List<BoxDetailVo> boxDetailVos = boxService.listMyBox(UserContext.getUserId());
        return RestResponse.success(boxDetailVos);
    }

    /**
     * 分页获取音乐盒数据
     *
     * @param queryVo {}
     * @return BoxVo
     */
    @PostMapping("/pageByVo")
    public RestResponse<PageVo<BoxVo>> pageByVo(@RequestBody BoxQueryVo queryVo) {
        AssertUtils.checkQueryPageVo(queryVo);
        queryVo.setUserId(UserContext.getUserId());
        return RestResponse.success(boxService.pageByVo(queryVo));
    }

    /**
     * 删除歌单
     *
     * @param deleteVo {}
     * @return Boolean
     */
    @PostMapping("/deleteById")
    @RequestLimit(count = 5)
    public RestResponse<Boolean> deleteById(@RequestBody @Validated DeleteVo deleteVo) {
        boxService.deleteById(deleteVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 模糊搜索建议数据
     *
     * @param name String
     * @return String
     */
    /*@GetMapping("/searchLikeName/{name}")
    public RestResponse<List<String>> searchLikeName(@PathVariable(value = "name") String name) {
        if (StringUtils.isBlank(name)) {
            return RestResponse.success(Lists.newArrayList());
        }
        final SearchHits<BoxEsEo> searchHits = boxService.searchLikeName(name);
        final List<String> strings = searchHits.getSearchHits().stream().map(searchHit -> searchHit.getContent().getName()).toList();
        return RestResponse.success(strings);
    }*/

    /**
     * 分页搜索
     *
     * @param queryVo {}
     * @return BoxVo
     */
    /*@PostMapping("/searchByVo")
    public RestResponse<PageVo<BoxVo>> searchByVo(@RequestBody @Validated BoxQueryEsVo queryVo) {
        PageVo<BoxVo> pageVo = new PageVo<>();
        AssertUtils.checkQueryPageVo(queryVo);
        final SearchHits<BoxEsEo> searchHits = boxService.searchByVo(queryVo);
        pageVo.setTotal(searchHits.getTotalHits());
        final List<BoxVo> boxVoList = searchHits.getSearchHits().stream().map(searchHit ->
                SmartBeanCopy.copy(searchHit.getContent(), BoxVo.class)
                        .setHighlightName(searchHit.getHighlightField("name").get(0))).toList();
        pageVo.setRecords(boxVoList);
        return RestResponse.success(pageVo);
    }*/
}
